package cn.wolfcode.business.controller;

import cn.wolfcode.business.domain.BusServiceItem;
import cn.wolfcode.business.service.IBusServiceItemService;
import cn.wolfcode.business.vo.AuditInfoVO;
import cn.wolfcode.business.vo.AuditVO;
import cn.wolfcode.common.core.controller.BaseController;
import cn.wolfcode.common.core.domain.AjaxResult;
import cn.wolfcode.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/business/serviceitem")
public class BusServiceItemController extends BaseController {
    @Autowired
    private IBusServiceItemService itemService;

    //服务项列表 分页查询
    @GetMapping("/list")
    public TableDataInfo list(BusServiceItem busServiceItem){
        //设置分页
        startPage();
        List<BusServiceItem> busServiceItems = itemService.selectBusServiceItemList(busServiceItem);
        return getDataTable(busServiceItems);
    }

    //服务项列表 非分页查询
    @GetMapping(value = "/list" ,headers = "cmd=list")
    public AjaxResult listForStatementItem(BusServiceItem busServiceItem){
        List<BusServiceItem> busServiceItems = itemService.selectBusServiceItemList(busServiceItem);
        return AjaxResult.success(busServiceItems);
    }

    //服务项新增
    @PostMapping
    public AjaxResult add(@RequestBody BusServiceItem busServiceItem){
        return toAjax(itemService.insertBusServiceItem(busServiceItem));
    }

    /**
     * 按照id查询
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(itemService.selectBusServiceItemById(id));
    }

    //服务项编辑
    @PutMapping
    public AjaxResult edit(@RequestBody BusServiceItem busServiceItem){
        return toAjax(itemService.updateBusServiceItem(busServiceItem));
    }

    //上架
    @PatchMapping(value = "/{id}",headers = "cmd=saleOn")
     public AjaxResult saleOn(@PathVariable Long id){
        return toAjax(itemService.saleOn(id));
    }

    //上架
    @PatchMapping(value = "/{id}",headers = "cmd=saleOff")
    public AjaxResult saleOff(@PathVariable Long id){
        return toAjax(itemService.saleOff(id));
    }

    //删除
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id){
        return toAjax(itemService.deleteBusServiceById(id));
    }

    //查询审批页面数据
    @GetMapping("/{id}/auditInfos")
    public AjaxResult getAuditInfo(@PathVariable Long id){
        AuditInfoVO vo = itemService.getAuditInfo(id);
        return AjaxResult.success(vo);
    }

    //发起审批
    @PostMapping("/auditInfos")
    public AjaxResult startAudit(@RequestBody AuditVO vo){
        return toAjax(itemService.startAudit(vo));
    }

}
